﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbConnect.Sql;

namespace DnDEngine.Class
{

    /// <summary>
    /// Paramter will be the most important part of the WebEngine
    /// It will give freedom to the user for his request.
    /// Paramter will have a Name and a value.
    /// Request will have the name of each parameters, and when we execute the request,
    /// we will replace name by value.
    /// Let's have a little example.
    /// Website : www.google.fr
    /// Request : http://www.google.fr/search?gcx=<?Q>&sourceid=<?NAV>&ie=<?LNG>&q=<?WHO>
    /// So we have 4 parameters define with <?Name>
    /// When we execute, we're going to replace the parameters name with correct value.
    /// So, we have to ask user which are the value of parameters.
    /// Value can be dynamic or static.
    /// In this example, the only dynamic parameter is <?Q>, which is the search keyword.
    /// So we're going to ask the user to enter the value, and then replace it.
    /// every parameter will be saved in database for future use.
    /// </summary>
    public class Parameter
    {
        #region "Private attributes"

        private int _ID;

        #endregion

        #region "Properties"

        public String Value { get; set; }
        private String _Name;
        public String Name
        {
            get
            { return _Name; }
            set
            {
                _Name = value;
                CheckName();
            }
        }

        #endregion

        #region "Events"
        #endregion

        #region "Public functions"

        public Parameter(String name, String value)
        {
            this.Name = name;
            this.Value = value;
            //Insert param
        }

        public Parameter(Int16 ID)
        {
            this._ID = ID;
            this.Load();
        }


        public void Delete()
        {
            try
            {
                Logger.Log.TraceAction("Parameter.Delete", "");
                var req = SqlParameter.Delete;
                var data = new DbConnect.DataAccess();
               
                 data.AddParam(ParamParameter.ID, this._ID);
                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Parameter.Delete", ex);
            }
            finally
            {

            }
        }

        public void Load()
        {
            try
            {
                Logger.Log.TraceAction("Parameter.Load", "");
                var req = SqlParameter.Select;
               
                var data = new DbConnect.DataAccess();
                 data.AddParam(ParamParameter.ID, this._ID);
                var table = data.SelectWithParam(req);

                if (table.Rows.Count > 0)
                {
                    this.Name = table.Rows[0]["ParameterLib"].ToString();
                    this.Value = table.Rows[0]["ParameterDefaultValue"].ToString();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Parameter.Load", ex);
            }

        }

        #endregion

        #region "Private functions"
        //Each param name must have an ? at position 0.
        private void CheckName()
        {
            try
            {
                Logger.Log.TraceAction("Parameter.CheckName", "");

                if (!Name[0].Equals('?'))
                {
                    this.Name = "?" + this.Name;
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Parameter.CheckName", ex);
            }

        }

        #endregion



    }
}
